package com.itheima.travel.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.session.SubjectUser;
import com.itheima.travel.session.SubjectUserContext;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class FavoriteServiceImpl implements FavoriteService {
    //session域
    @Autowired
    private SubjectUserContext subjectUserContext;
    //线路
    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private RouteMapperExt routeMapperExt;
   //收藏
    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private FavoriteMapperExt favoriteMapperExt;
    @Autowired
    private RedisCacheService redisCacheService;
    @Autowired
    private AffixService affixService;

    //分页查询
    @Override
    public PageInfo<RouteVo> findMyFavorite(FavoriteVo favoriteVo, int pageNumber, int pageSize) {

        //使用分页,初始的当前页，每页显示多少条
        PageHelper.startPage(pageNumber,pageSize);
        //获取当前登录的用户信息
        SubjectUser subject = subjectUserContext.getSubject();
        //查询用户的收藏的线路id，并查询出对应的线路信息
        List<Route> routeList = favoriteMapperExt.findFavoriteByUserId(subject.getId());
        //利用分页插件进行一系列分页操作
        PageInfo<Route> routePageInfo = new PageInfo<>(routeList);
        PageInfo<RouteVo> routeVoPageInfo = new PageInfo<>();
        //将info类型转化为infoVo类型的数据
        BeanConv.toBean(routePageInfo,routeVoPageInfo);
        //都转为vo类型
        List<RouteVo> listVo = BeanConv.toBeanList(routeList, RouteVo.class);
        routeVoPageInfo.setList(listVo);
        //处理图片
        for (RouteVo routeVo : listVo) {
            //根据线路id获取每个线路的图片信息，再将图片信息存放到对应的对象中保存
            routeVo.setAffixVoList(affixService.findAffixBuBusinessId(AffixVo.builder().businessId(routeVo.getId()).build()));
        }
        return routeVoPageInfo;
    }

    //是否收藏过
    @Transactional
    @Override
    public Boolean isFavorite(FavoriteVo favoriteVo) {
        //若为登录状态，则直接从session域中获取
        SubjectUser subject = subjectUserContext.getSubject();
        System.out.println(subject);
        //根据查询条件
        FavoriteExample favoriteExample = new FavoriteExample();
        //创建例子给条件(路线id和用户id)
        favoriteExample.createCriteria().
                andRouteIdEqualTo(favoriteVo.getRouteId()).
                andUserIdEqualTo(subject.getId());
        //根据条件，返回查找结果
        List<Favorite> favoriteList = favoriteMapper.selectByExample(favoriteExample);
        //如果收藏过返回true，否则返回false
        return !EmptyUtil.isNullOrEmpty(favoriteList);

    }
    //添加收藏
    @Transactional
    @Override
    public Integer addFavorite(FavoriteVo favoriteVo) {
        //首先从session中获取用户信息，将收藏绑定用户信息
        SubjectUser subject = subjectUserContext.getSubject();
        //获取对象id并设置为收藏用户的id
        favoriteVo.setUserId(subject.getId());
        //TODO 向收藏表中插入语句，将收藏的线路与自己做绑定
        int favoriteInsert = favoriteMapper.insert(BeanConv.toBean(favoriteVo, Favorite.class));
        //判断是否添加收藏成功
        if(favoriteInsert==0){
            throw new RuntimeException("添加收藏失败");
        }
        //TODO 根据路线id进行修改，收藏数据加1
        Long routeUpdate = routeMapperExt.updateRouteCountById(favoriteVo.getRouteId());
        //判断是否添加成功
        if(routeUpdate==0){
            //添加失败
            throw new RuntimeException("修改统计表出错");
        }
        //如果收藏成功，并且添加成功，则展示线路信息
        Route route = routeMapper.selectByPrimaryKey(favoriteVo.getRouteId());
        //返回当前收藏的总数据
        return route.getAttentionCount();
    }
}
